package com.un.ebs.invoice.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.exception.BusinessEntityNotFoundException;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.invoice.domain.*;
import com.un.ebs.invoice.dto.*;
import com.un.ebs.invoice.enums.InvoiceTypeEnum;
import com.un.ebs.invoice.service.InvoiceNoteService;
import com.un.ebs.sale.dto.SaleOrderNotInvoiceView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (InvoiceNote)表控制层
 *
 * @author liubo
 * @since 2020-09-27 11:06:42
 */
@RestController
@RequestMapping("invoiceNote")
@Api(value = "invoice-note")
public class InvoiceNoteController {
    /**
     * 服务对象
     */
    @Resource
    private InvoiceNoteService service;

    @PostMapping("save")
    @ApiOperation(value = "保存")
    public WebResult save(@RequestBody @Validated InvoiceNoteUpdateDto req) {
        service.save(req);
        return WebUtils.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public WebResult delete(@RequestParam String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("submit")
    @ApiOperation(value = "提交")
    public WebResult submit(@RequestParam String id) {
        service.submit(id);
        return WebUtils.success();
    }

    @PostMapping("submitCancel")
    @ApiOperation(value = "提交撤销")
    public WebResult submitCancel(@RequestParam String id) {
        service.submitCancel(id);
        return WebUtils.success();
    }

    @PostMapping("audit")
    @ApiOperation(value = "审核")
    public WebResult audit(@RequestBody @Validated InvoiceAuditDto req) {
        service.audit(req);
        return WebUtils.success();
    }

    @PostMapping("auditCancel")
    @ApiOperation(value = "审核撤销")
    public WebResult auditCancel(@RequestParam String id) {
        service.auditCancel(id);
        return WebUtils.success();
    }

    @PostMapping("back")
    @ApiOperation(value = "发票冲销")
    public WebResult back(@RequestParam String id, @RequestParam(required = false) String remark) {
        service.back(id, remark);
        return WebUtils.success();
    }

    @PostMapping("cash")
    @ApiOperation(value = "现金记账")
    public WebResult cash(@RequestBody @Validated CashDto req) {
        service.cash(req.getEntId(), req.getMoney(), req.getCashDate(), req.getType(), req.getMoneyDict(), req.getRemark());
        return WebUtils.success();
    }

    @PostMapping("deleteCash")
    @ApiOperation(value = "删除现金记账")
    public WebResult deleteCash(@RequestParam String id) {
        service.deleteCash(id);
        return WebUtils.success();
    }

    @PostMapping("deleteCashClaim")
    @ApiOperation(value = "删除现金认领")
    public WebResult deleteCashClaim(@RequestParam String id) {
        service.deleteCashClaim(id);
        return WebUtils.success();
    }

    @PostMapping("cashClaim")
    @ApiOperation(value = "现金认领")
    public WebResult cashClaim(@RequestParam String cashNoteId, @RequestParam String invoiceNoteId, @RequestParam Double claimMoney) {
        service.cashClaim(cashNoteId, invoiceNoteId, claimMoney);
        return WebUtils.success();
    }

    @PostMapping("listViewById")
    @ApiOperation(value = "详情")
    public WebResult<InvoiceNoteVo> queryViewById(@RequestParam String id) {
        return WebUtils.success(service.queryViewById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "查询")
    public WebResult<IPage<InvoiceNoteExt>> query(@RequestParam InvoiceTypeEnum invoiceType, @RequestParam Integer accountYear, @RequestParam Integer accountMonth, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        if (invoiceType == InvoiceTypeEnum.Purchase) {
            return WebUtils.success(service.queryPurchase(accountYear, accountMonth, pageSize, pageIndex));
        } else {
            return WebUtils.success(service.query(invoiceType, accountYear, accountMonth, pageSize, pageIndex));
        }
    }

    @GetMapping("listWaitAudit")
    @ApiOperation(value = "查询，待审核发票")
    public WebResult<IPage<InvoiceNoteExt>> queryWaitAudit(@RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryWaitAudit(pageSize, pageIndex));
    }

    @GetMapping("listNotCash")
    @ApiOperation(value = "查询，未结算的发票")
    public WebResult<IPage<InvoiceNoteExt>> queryNotCash(@RequestParam String entId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryNotCash(entId, pageSize, pageIndex));
    }

    @GetMapping("listMyNotCash")
    @ApiOperation(value = "查询，未结算的发票")
    public WebResult<IPage<InvoiceNoteExt>> queryMyNotCash(@RequestParam String cashId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        InvoiceCashNoteExt obj = service.queryCashViewById(cashId);
        if (obj != null) {
            return WebUtils.success(service.queryNotCash(obj.getEntId(), pageSize, pageIndex));
        } else {
            return WebUtils.error(new BusinessEntityNotFoundException(cashId, "回款信息"));
        }

    }

    @GetMapping("listMyInvoice")
    @ApiOperation(value = "查询，当前用户名下的所有发票")
    public WebResult<IPage<InvoiceNoteExt>> queryMyInvoice(@RequestParam(required = false) Boolean isCash, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryMyInvoice(isCash, pageSize, pageIndex));
    }

    @GetMapping("listItem")
    @ApiOperation(value = "查询，货物明细")
    public WebResult<List<InvoiceNoteItemExt>> queryItem(@RequestParam String noteId) {
        InvoiceNoteVo note = service.queryViewById(noteId);
        if (note != null) {
            if (note.getInvoiceType() == InvoiceTypeEnum.Purchase) {
                return WebUtils.success(service.queryPurchaseItem(noteId));
            } else {
                return WebUtils.success(service.queryItem(noteId));
            }
        } else {
            return WebUtils.success();
        }
    }

    @GetMapping("listItemBySale")
    @ApiOperation(value = "查询，服务明细")
    public WebResult<IPage<InvoiceNoteItemBySaleExt>> queryItemBySale(@RequestParam String noteId, @RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryItemBySale(noteId, pageSize, pageIndex));
    }

    @PostMapping("listItemByQuery")
    @ApiOperation(value = "查询，所有明细，包含货物和服务类")
    public WebResult<IPage<InvoiceNoteItemAllExt>> listItemByQuery(@RequestBody @Validated InvoiceItemQueryRequest req) {
        return WebUtils.success(service.queryItem(req));
    }

    @GetMapping("listCash")
    @ApiOperation(value = "查询，现金流水")
    public WebResult<IPage<InvoiceCashNoteExt>> queryCash(@RequestParam(required = false) String entId, @RequestParam(required = false) Integer cashYear, @RequestParam(required = false) Integer cashMonth, @RequestParam(required = false) Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryCash(entId, cashYear, cashMonth, pageSize, pageIndex));
    }

    @GetMapping("listMyCash")
    @ApiOperation(value = "查询，当前用户的现金流水")
    public WebResult<IPage<InvoiceCashNoteExt>> queryMyCash(@RequestParam(required = false) Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.queryCash(pageSize, pageIndex));
    }

    @GetMapping("listCashClaim")
    @ApiOperation(value = "查询，现金流水")
    public WebResult<List<InvoiceCashNoteItemExt>> queryCashClaim(@RequestParam String id) {
        return WebUtils.success(service.queryCashClaim(id));
    }

    @GetMapping("listCashInfo")
    @ApiOperation(value = "查询，现金流水详情")
    public WebResult<InvoiceCashVo> queryCashInfo(@RequestParam String cashId) {
        return WebUtils.success(service.queryCashInfo(cashId));
    }

    @GetMapping("listSaleNotInvoice")
    @ApiOperation(value = "查询，销售出库未开票记录汇总")
    public WebResult<IPage<SaleNotInvoiceExt>> querySaleNotInvoice(@RequestParam Integer pageSize, @RequestParam Integer pageIndex) {
        return WebUtils.success(service.querySaleNotInvoice(pageSize, pageIndex));
    }

    @GetMapping("listInvoiceBalance")
    @ApiOperation(value = "查询，可开发票")
    public WebResult<List<InvoiceBalanceDto>> queryInvoiceBalance(@RequestParam(required = false) String entName, @RequestParam InvoiceTypeEnum type) {
        return WebUtils.success(service.queryInvoiceBalance(entName, type));
    }

    @GetMapping("listInvoiceBalanceByService")
    @ApiOperation(value = "查询，可开服务类的发票")
    public WebResult<List<SaleOrderNotInvoiceView>> queryInvoiceBalanceByService() {
        return WebUtils.success(service.querySaleNotInvoiceByService());
    }

    @GetMapping("listAccountAge")
    @ApiOperation(value = "查询，应收款")
    public WebResult<List<AccountAgeDto>> queryAccountAgeExt() {
        return WebUtils.success(service.queryAccountAge());
    }
}